import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/subject_dao.dart';
import 'package:hktk_app/model/home/column_model.dart';
import 'package:hktk_app/model/page/resource_page_model.dart';

//应该传入所选栏目，科目，考试，行业,点击跳转PaperDetailPage
class PaperLibrary extends StatefulWidget {
  final int categoryId;
  final String industryId;
  final String examId;
  final ColumnModel columnModel;

  const PaperLibrary({
    super.key,
    required this.categoryId,
    required this.industryId,
    required this.examId,
    required this.columnModel,
  });

  @override
  State<StatefulWidget> createState() => _PaperLibraryState();
}

class _PaperLibraryState extends State<PaperLibrary> {
  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(widget.columnModel.name!),
        ),
        body: SafeArea(
            child: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          child: FutureBuilder(future: () async {
            return SubjectDao.paperByColumnId(
                size: 100,
                current: 1,
                columnId: widget.columnModel.id!,
                countDetail: true);
          }(), builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              var data = snapshot.data as ResourcePageModel;
              if (data.records!.isEmpty) {
                return Center(
                  child: SvgPicture.asset(
                    "images/home/empty.svg",
                    width: 1.sw - 20,
                  ),
                );
              } else {
                return Container(
                  height:
                      1.sh - ScreenUtil().statusBarHeight - kToolbarHeight - 30,
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        for (var i=0;i< data.records!.length;i++) ...[
                          Padding(
                            padding: EdgeInsets.only(
                                left: 14, right: 14, top: 14, bottom: 14),
                            child: Row(
                              spacing: 10,
                              mainAxisSize: MainAxisSize.max,
                              children: [
                                Expanded(
                                    child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  spacing: 5,
                                  children: [
                                    Text(data.records![i].name),
                                    Row(
                                      mainAxisSize: MainAxisSize.max,
                                      spacing: 8,
                                      children: [
                                        Text.rich(TextSpan(children: [
                                          TextSpan(text: "总题数"),
                                          WidgetSpan(child: SizedBox(width: 5)),
                                          TextSpan(text: data.records![i].qcount.toString())
                                        ])),
                                        Container(
                                            width: 2,
                                            height: 16,
                                            // color: Colors.grey,
                                            child: VerticalDivider(
                                              width: 2,
                                              color: Colors.grey,
                                            )),
                                        Text.rich(TextSpan(children: [
                                          TextSpan(
                                              text: (data.records![i].stuCount ?? 0)
                                                  .toString()),
                                          WidgetSpan(child: SizedBox(width: 5)),
                                          TextSpan(text: "人做过")
                                        ]))
                                      ],
                                    )
                                  ],
                                )),
                                TextButton(
                                    style: TextButton.styleFrom(
                                      padding: EdgeInsets.only(
                                          top: 3,
                                          bottom: 5,
                                          left: 25,
                                          right: 25),
                                      shape: RoundedRectangleBorder(
                                        borderRadius:
                                            BorderRadius.circular(30.0),
                                      ),
                                      side: BorderSide(
                                          color: Color(0xFF3C6CFF), width: 1),
                                    ),
                                    onPressed: () {
                                      Navigator.pushNamed(context, "/paper-detail",arguments: {
                                        "id":data.records![i].id,
                                        "columnId":widget.columnModel.id,
                                        "recordId":data.records![i].recordId,
                                        "index":i,
                                        "categoryId":widget.categoryId,
                                      });
                                    },
                                    child: Text(
                                      "做题",
                                      style: TextStyle(
                                          fontSize: 16,
                                          color: Color(0xFF3C6CFF)),
                                    ))
                              ],
                            ),
                          )
                        ],
                      ],
                    ),
                  ),
                );
              }
            } else {
              return Center(child: CircularProgressIndicator());
            }
          }),
        )));
  }
}
